package com.ruoyi.back.service.v3.impl

import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.back.domain.v3.SensitiveWord
import com.ruoyi.back.domain.v3.SensitiveWordRecord
import com.ruoyi.back.mapper.SensitiveWordRecordMapper
import com.ruoyi.back.service.AppUserService
import com.ruoyi.back.service.v3.SensitiveWordRecordDto
import com.ruoyi.back.service.v3.SensitiveWordRecordSearchDto
import com.ruoyi.back.service.v3.SensitiveWordRecordService
import com.ruoyi.back.service.v3.SensitiveWordService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class SensitiveWordRecordServiceImpl : ServiceImpl<SensitiveWordRecordMapper, SensitiveWordRecord>(),
    SensitiveWordRecordService {

    @Autowired
    private lateinit var appUserService: AppUserService

    @Autowired
    private lateinit var sensitiveWordService: SensitiveWordService

    override fun selectSensitiveWordRecordList(sensitiveWordRecordSearchDto: SensitiveWordRecordSearchDto): Page<SensitiveWordRecord> {

        return TableSupport.buildPageRequest().let { pageDomain ->
            page(
                Page(pageDomain.pageNum.toLong(), pageDomain.pageSize.toLong()),
                KtQueryWrapper(SensitiveWordRecord::class.java)
                    .eq(
                        StrUtil.isNotBlank(sensitiveWordRecordSearchDto.userInfo),
                        SensitiveWordRecord::userId,
                        sensitiveWordRecordSearchDto.userInfo
                    )
                    .or()
                    .eq(
                        StrUtil.isNotBlank(sensitiveWordRecordSearchDto.userInfo),
                        SensitiveWordRecord::username,
                        sensitiveWordRecordSearchDto.userInfo
                    )
                    .like(
                        StrUtil.isNotBlank(sensitiveWordRecordSearchDto.prompt),
                        SensitiveWordRecord::prompt,
                        sensitiveWordRecordSearchDto.prompt
                    )
                    .orderByDesc(SensitiveWord::createTime)
            )
        }
    }

    override fun saveSensitiveWordRecord(sensitiveWordRecordDto: SensitiveWordRecordDto): Boolean {

        return sensitiveWordService.getSensitiveWord()?.let {
            it.map { sensitiveWord ->
                sensitiveWord?.split(",")
                    ?.filter { StrUtil.isNotBlank(it) }
                    ?.filter { sensitiveWordRecordDto.prompt?.contains(it) ?: false }
                    ?.takeIf { it.isNotEmpty() }
                    ?.run {
                        filter { StrUtil.isNotBlank(it) }
                            .forEach { word ->
                                save(SensitiveWordRecord().apply {
                                    appUserService.getById(sensitiveWordRecordDto.createBy)
                                        ?.let {
                                            userId = it.userId
                                            username = it.username
                                        }
                                    this.sensitiveWord = word
                                    prompt = sensitiveWordRecordDto.prompt
                                })
                            }
                        true
                    }
                    ?: false
            }.any { it }
        } ?: false
    }
}